﻿using BroadGoalComponents.BGControls.Toast;
using BroadGoalComponents.Dialogs;
using Custom.Framework;
using Custom.Infrastructure.EventBus;
using Custom.Model.Entity;
using Custom.Service.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Business.Sale
{
    public partial class frmSaleList : Form
    {
        // 当前客户
        private int cusid = 0;
        private SaleInfoRepository saleDAL = new SaleInfoRepository();
        public bool ShowCover { get; set; } = true;

        public frmSaleList()
        {
            InitializeComponent();

            this.bgDataGridView1.TopLeftHeaderCell.Value = "序号";
            this.bgDataGridView1.RowHeadersWidth = 55;
            this.bgDataGridView1.RowHeadersVisible = true;
            this.bgDataGridView1.ShowLineNo = true;
        }

        public frmSaleList(int cusid)
        {
            InitializeComponent();

            this.cusid = cusid;

            this.bgDataGridView1.TopLeftHeaderCell.Value = "序号";
            this.bgDataGridView1.RowHeadersWidth = 55;
            this.bgDataGridView1.RowHeadersVisible = true;
            this.bgDataGridView1.ShowLineNo = true;

            // 隐藏查询条件
            this.label1.Visible = false;
            this.label2.Visible = false;
            this.label3.Visible = false;
            this.txtCardNo.Visible = false;
            this.txtRealName.Visible = false;
            this.txtTel.Visible = false;
            this.btnSearch.Visible = false;
            this.bgButtonBar1.Location = new Point(this.bgButtonBar1.Location.X, this.bgButtonBar1.Location.Y - 54);
            this.bgDataGridView1.Location = new Point(this.bgDataGridView1.Location.X, this.bgDataGridView1.Location.Y - 54);
        }

        private void frmSaleList_Load(object sender, EventArgs e)
        {
            this.search();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.search();
        }

        /// <summary>
        /// 新增订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.ShowCover) this.showCover();

            frmSaleAdd frm = new frmSaleAdd();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (this.ShowCover) this.hideCover();

                this.search();
            }
            else
            {
                if (this.ShowCover) this.hideCover();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<long> ids = this.bgDataGridView1.GetCheckedId<long>(this.col_chekflag.Name, this.col_id.Name);
            if (ids.Count == 0)
            {
                WarningDialog.Show(this, "请至少选择一条销售记录");
                return;
            }

            DialogResult dialogResult = QuestionDialog.Show("是否确认删除？", "数据删除后不可恢复，请谨慎操作！");
            if (dialogResult == DialogResult.Yes)
            {
                this.saleDAL.BatchDelete(ids);
                this.search();
                BGToast.Show("删除完成");
            }
        }

        /// <summary>
        /// 格式化显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bgDataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == this.col_totalamount.Index)
            {
                e.Value = decimal.Parse(e.Value.ToString()).ToString("0.00");
            }
        }

        /// <summary>
        /// 操作列按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bgDataGridView1_ActionButtonClick(object sender, BroadGoalComponents.BGControls.ActionButtonEventArgs e)
        {
            int rowIndex = this.bgDataGridView1.CurrentRow.Index;

            switch (e.ButtonName)
            {
                case "修改":
                    this.edit(rowIndex);
                    break;
                case "查看":
                    this.view(rowIndex);
                    break;
            }
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bgPagination_Click(object sender, EventArgs e)
        {
            this.search();
        }

        /// <summary>
        /// 总页码更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bgPagination_OnPageSizeChanged(object sender, EventArgs e)
        {
            this.bgPagination.CurrentPage = 1;
            this.search();
        }

        /// <summary>
        /// 窗体大小变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSaleList_SizeChanged(object sender, EventArgs e)
        {
            this.lbDataCount.Location = new Point(this.bgPagination.Location.X + this.bgPagination.Width + 10, this.lbDataCount.Location.Y);
        }

        /// <summary>
        /// 查询
        /// </summary>
        private void search()
        {
            try
            {
                List<SaleInfo> sales = this.saleDAL.GetAllSaleInfos();

                // 卡号
                string cardNo = this.txtCardNo.Text.Trim();
                if (!string.IsNullOrEmpty(cardNo))
                {
                    sales = sales.Where(w => w.cardno.Contains(cardNo)).ToList();
                }
                // 姓名
                string realName = this.txtRealName.Text.Trim();
                if (!string.IsNullOrEmpty(realName))
                {
                    sales = sales.Where(w => w.cusname.Contains(realName)).ToList();
                }
                // 联系电话
                string tel = this.txtTel.Text.Trim();
                if (!string.IsNullOrEmpty(tel))
                {
                    sales = sales.Where(w => w.mobile.Contains(tel)).ToList();
                }

                if (this.cusid > 0)
                {
                    sales = sales.Where(w => w.cusid == this.cusid).ToList();
                }

                // 显示当前页的数据源
                this.lbDataCount.Text = $"共{sales.Count}条数据";
                // 分页控件数据绑定
                this.bgPagination.TotalPage = (sales.Count - 1) / this.bgPagination.PageSize + 1;
                if (this.bgPagination.CurrentPage >= this.bgPagination.TotalPage)
                {
                    this.bgPagination.CurrentPage = this.bgPagination.TotalPage > 0 ? this.bgPagination.TotalPage : 1;
                }
                this.lbDataCount.Location = new Point(this.bgPagination.Location.X + this.bgPagination.Width + 10, this.lbDataCount.Location.Y);

                // 绑定当前页的数据源
                this.bgDataGridView1.DataSource = sales.Skip(this.bgPagination.PageSize * (this.bgPagination.CurrentPage - 1)).Take(this.bgPagination.PageSize).ToList();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                WarningDetailDialog.Show(this, "查询失败！", ex.Message);
            }
        }

        private void edit(int rowIndex)
        {
            long saleid = long.Parse(this.bgDataGridView1.Rows[rowIndex].Cells[this.col_id.Name].Value.ToString());

            if (this.ShowCover) this.showCover();

            frmSaleEdit frm = new frmSaleEdit(saleid);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (this.ShowCover) this.hideCover();
                this.search();
            }
            else
            {
                if (this.ShowCover) this.hideCover();
            }
        }

        private void view(int rowIndex)
        {
            long saleid = long.Parse(this.bgDataGridView1.Rows[rowIndex].Cells[this.col_id.Name].Value.ToString());

            if (this.ShowCover) this.showCover();

            frmSaleView frm = new frmSaleView(saleid);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (this.ShowCover) this.hideCover();
                this.search();
            }
            else
            {
                if (this.ShowCover) this.hideCover();
            }
        }

        private void showCover()
        {
            EventBus.Instance.Publish(new TEventEntity()
            {
                ActionName = "showCover"
            }, callback);
        }

        private void hideCover()
        {
            EventBus.Instance.Publish(new TEventEntity()
            {
                ActionName = "hideCover"
            }, callback);
        }

        private void callback(TEventEntity arg1, bool arg2, Exception arg3)
        {

        }
    }
}
